{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa7ed0df-02b9-4c3c-b349-dc655ce45b39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一、语料"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a56b5682065f9aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "\"\"\"将txt每行作为一个单元格转换为只有一列的表格\"\"\"\n",
    "raw = pd.read_csv(\n",
    "    \"../code/novel.txt\", # 读取文件\n",
    "    names = ['txt'],\n",
    "    sep ='aaa',  # 分隔符，必须设置为文章中没有出现过的字符串\n",
    "    encoding =\"GBK\",\n",
    "    engine='python'\n",
    ")\n",
    "# print(len(raw)) # 行数\n",
    "# print(raw)\n",
    "\n",
    "\"\"\"对raw表格的txt列的每一行调用函数新建其他列\"\"\"\n",
    "raw['char1st'] = raw.txt.apply(lambda line: line[0])\n",
    "raw['index'] = raw.txt.apply(lambda line: line.find('回 '))\n",
    "raw['length'] = raw.txt.apply(len)\n",
    "raw['ch_num'] = 0 # 将char_num设置为整数，防止默认设置为小数\n",
    "\n",
    "\"\"\"章节判断\"\"\"\n",
    "\n",
    "ch_num = 0\n",
    "# 遍历小说所有行，并注明这个单元格所属的章节号\n",
    "for i in range(len(raw)):\n",
    "    if raw['char1st'][i] == \"第\" and raw['index'][i] > 0 and raw['length'][i] < 30 :\n",
    "        # 查找有 \"第**回 \" 并且文本数少于30的行，一般每章标题不会超过30字\n",
    "        ch_num += 1\n",
    "    if ch_num >= 40 and raw['txt'][i] == \"附录一：成吉思汗家族\" :\n",
    "        # 正文总共40回，这里只需要正文内容，不需要前言附录等\n",
    "        char_num = 0\n",
    "    raw.loc[i, 'ch_num'] = ch_num\n",
    "\n",
    "\"\"\"删除辅助列\"\"\"\n",
    "del raw['char1st']\n",
    "del raw['index']\n",
    "del raw['length']\n",
    "\n",
    "# print(raw.head(50))\n",
    "\n",
    "# print(raw[raw.ch_num == 1].head())\n",
    "# raw.txt.transform(len).plot.box()\n",
    "# plt.show()\n",
    "\n",
    "# 按照ch_num列分组\n",
    "rawgrp = raw.groupby('ch_num')\n",
    "# 拼接同一组的内容为字符串\n",
    "chapter = rawgrp.agg('sum')\n",
    "# 过滤掉ch_num为0的章节\n",
    "chapter = chapter[chapter.index != 0]\n",
    "# print(chapter.txt[2])\n",
    "# print(chapter)\n",
    "\n",
    "def chapter_n(n: int) -> str:\n",
    "    return chapter[n]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
